# -*- coding: utf-8 -*-
"""
name:额度-重庆_预审批
code:pricing_amount_CF_1
@author: 张恒峰
"""

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

#%%现有参数导入
#指标缺失值转化为0
def null_trans(index_name):
    if index_name == u"缺失值":
        return 0.0
    else:
        try:
            value = float(index_name)
        except:
            value = 0.0
        return value

#年均申报收入
Avg_Year_Income ={TX02_MonthlyIncomeReportAmt}

#评级等级
rating_level = decisionModelResult.get("ratingLevel").lower()

#近1年出口收汇金额总额
CF18_Mon12ARAmount = null_trans({CF18_Mon12ARAmount})

#汇率
CF19_XECurrencyOfUSD = null_trans({CF19_XECurrencyOfUSD})

#近3年年均收汇率
CF12_AvgARRatioInThree = null_trans({CF12_AvgARRatioInThree})
if 1 < CF12_AvgARRatioInThree <= 1.4:
	CF12_AvgARRatioInThree = 1
elif 1.4 < CF12_AvgARRatioInThree <= 1.5:
	CF12_AvgARRatioInThree = 0.9
elif CF12_AvgARRatioInThree > 1.5:
	CF12_AvgARRatioInThree = 0.8
else:
	pass


#%%计算调整系数
credit_line_dict = {"A":{'a':1.2,'b':1.2,'c':1.2,'d':1.2,'e':1.16,'f':0},\
                    "B":{"a":1.2,"b":1.16,"c":1.14,"d":1.12,'e':1.1,'f':0},\
                    "C":{'a':1.16,'b':1.12,'c':1.1,'d':1.04,'e':0.96,'f':0},\
                    "D":{"a":1.12,"b":1.08,"c":1.04,"d":0.98,'e':0.92,'f':0},\
                    "E":{"a":1.08,"b":1.04,"c":1,"d":0.9,'e':0.84,'f':0},\
                    "F":{"a":1.04,"b":1,"c":0.92,"d":0.86,'e':0.8,'f':0},\
                    u"缺失值":{"a":0,"b":0,"c":0,"d":0,'e':0,'f':0}}

def tax_rating(x):
    try:
        if x>1300000:
            return "A"
        elif x>1000000 and x<=1300000:
            return "B"
        elif x>800000 and x<=1000000:
            return "C"
        elif x>500000 and x<=800000:
            return "D"
        elif x>20000 and x<=500000:
            return "E"
        else:
            return "F"
    except:
        return u"缺失值"

#月均销售收入评级
tax_level = tax_rating(Avg_Year_Income)

#获取调整系数
adjus_factor = credit_line_dict[tax_level][rating_level]
#%%授信额度
#part1_账期预计收汇额度*综合调整系数
adjust_amount = (CF18_Mon12ARAmount*CF19_XECurrencyOfUSD)/12*4*0.8*CF12_AvgARRatioInThree
#part2_申请额度
request_amount = float(ratingRequest.get('amount'))
#part3_产品最高额度（人民币
max_amount = 1000000

#汇总
credit_amount= min(request_amount,adjust_amount,max_amount)
credit_amount1=round(credit_amount/10000,1)*10000

result = credit_amount1